package weaver.interfaces.expense;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.impl.LogFactoryImpl;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetDataSource;
import weaver.file.LogMan;
import weaver.general.StaticObj;
import weaver.interfaces.datasource.DataSource;
import weaver.interfaces.schedule.BaseCronJob;
import weaver.workflow.action.BaseAction;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;


/**
 * Created by Administrator on 2017/6/8.
 * 每周定时同步预算,源码注释联系：345127857@qq.com
 */
public class OASynBudget extends BaseCronJob {
    @Override
    public void execute() {
            String sql = "我是可爱的SQL！！";
            Log log = LogFactory.getLog(OASynBudget.class);
            log.error(sql+"=========================================================");
            RecordSetDataSource resultSet = new RecordSetDataSource("ZJK");
//            DataSource ds = (DataSource) StaticObj.getServiceByFullname(("datasource.ZJK"),DataSource.class);
//            java.sql.Connection conn = ds.getConnection();
            sql = "select centitycode,centityname,cexnamed,cexcode,SUM(je)as je from sales_cos where iyear = (DateName(year,GetDate())) group by centitycode,centityname,cexnamed,cexcode";
                resultSet.execute(sql);

            //            ResultSet resultSet = conn.createStatement().executeQuery(sql);
            while (resultSet.next()){
                log.error("==========================同步数据111===============================");
                String centitycode = resultSet.getString("centitycode").trim();//部门编号
                String sybmmc = resultSet.getString("centityname").trim();//部门名称
                String fymc = resultSet.getString("cexnamed").trim();//费用名称
                String cexcode = resultSet.getString("cexcode").trim();//费用id
                String bzysje = resultSet.getString("je").trim();//总支出金额
                sql = "select * from EF_EP_V_EPBudget where centitycode = '"+centitycode+"' and cexpenseitemcode = '"+cexcode+"' and iyear = (DateName(year,GetDate()))";
                RecordSetDataSource rs1 = new RecordSetDataSource("ZJK");
                rs1.execute(sql);
//                DataSource ds2 = (DataSource)StaticObj.getServiceByFullname(("datasource.NC数据"),DataSource.class);
//                Connection conn2 = ds2.getConnection();
//                ResultSet rs1 = conn2.createStatement().executeQuery(sql);
                if (rs1.next()) {
                    //获取当前科室类别总预算
                    String dBBGAmount = rs1.getString("dBBGAmount");
                    System.out.println("===============查询到的总预算金额为" + dBBGAmount + "===============");
                    RecordSet rss = new RecordSet();
                    //更新OA中的预算台帐总预算
                    sql = "update uf_ystz set dBBGAmount = " + dBBGAmount + " where centitycode = '" + centitycode + "' and cexpenseitemcode = '" + cexcode + "'";
                    rss.execute(sql);
                    //更新OA中的预算台帐总已用预算
                    sql = "update uf_ystz set dsumamount = " + bzysje + " where centitycode = '" + centitycode + "' and cexpenseitemcode = '" + cexcode + "'";
                    rss.execute(sql);
                    //更新OA中的预算台帐余额
                    sql = "update uf_ystz set dcurrentamount = " + (Double.parseDouble(dBBGAmount) - Double.parseDouble(bzysje)) + " where centitycode = '" + centitycode + "' and cexpenseitemcode = '" + cexcode + "'";
                    rss.execute(sql);
                }
               // conn2.close();
            }
    }
}
